package com.yc.wxcontroller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.entity.StudentInfo;
import com.yc.entity.User;
import com.yc.mapper.AttendanceMapper;
import com.yc.mapper.SchoolYearMapper;
import com.yc.mapper.UserMapper;
import com.yc.service.AttendanceRecordsService;
import com.yc.service.impl.UserInfoServiceImpl;
import com.yc.vo.Result;
import com.yc.vo.AttendanceData;
import com.yc.vo.AttendanceHistory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zxy
 * @version 1.0
 * @date 2023/7/22 9:12
 */
@Api("微信学生信息控制器")
@RestController
public class WxStuInfoController {
    @Resource
    private SchoolYearMapper schoolYearMapper;
    @Resource
    private UserInfoServiceImpl userInfoService;
    @Resource
    private AttendanceRecordsService attendanceRecordsService;
    @Resource
    private AttendanceMapper attendanceMapper;
    @Resource
    private UserMapper userMapper;


    @ApiOperation("老师设置班委")
    @PostMapping("wx/setGoodStudent")
    List<Map<String, Object>> setGoodStudent(String tid,String stuid,String cid) {
        List<Map<String, Object>> allinfo = userInfoService.SetGoodStudent(tid,stuid,cid);
        return allinfo;
    }





    /**
     * 学生信息查询
     * @Author zxy
     * @param tid
     * @return 班长和学委的信息
     */
    @ApiOperation("查询当前老师'我的班级'的班长与学委信息")
    @PostMapping("wx/selectBorX")
    List<Map<String, Object>> selectBorX(String tid) {
        List<Map<String, Object>> allinfo = userInfoService.SelectBorXinfo(tid);
        return allinfo;
    }



    /**
     * 学生信息查询
     * @Author zxy
     * @param stuid
     * @return 学生电话 学生性别 辅导员 学生电话
     */
    @ApiOperation("查询学生信息 包括电话、性别、辅导员、学生电话")
    @PostMapping("wx/selectstuinfo")
    Map<String, Object> SelectStuInfo(String stuid) {
        Map<String, Object> stuinfo = userInfoService.SelectStuInfo(stuid);
        return stuinfo;
    }

    /**
     * 老师信息查询
     * @Author zxy
     * @param tid
     * @return 老师信息
     */
    @ApiOperation("查询当前老师的性别与电话")
    @PostMapping("wx/selectTinfo")
    Map<String, Object> SelectTeaInfo(String tid) {
        Map<String, Object> Tinfo = userInfoService.SelectTeaInfo(tid);
        return Tinfo;
    }
    /**
     * 修改学生的性别
     * @Author lzx
     * @paramid和sex
     * @return 是否成功 成功为1 失败为0
     */
    @ApiOperation("修改学生的性别")
    @PostMapping("wx/updateStudentSsex")
    Map<String,Object> updateStudentSex(@RequestParam("stuid") String  stuid,@RequestParam("ssex") Integer ssex){
        Map<String,Object> result=new HashMap<>();
        Integer i=userInfoService.updateStudentSex(stuid,ssex);
        if(i==0){
            result.put("msg",0);
        }else{
            result.put("msg",1);
        }
        return result;


    }
    /**
     * 修改学生手机号
     * @Author lzx
     * @paramid和sex
     * @return 是否成功 成功为1 失败为0
     */
    @ApiOperation("修改学生手机号")
    @PostMapping("wx/updateStudentPhone")
    Map<String,Object> updateStudentPhone(@RequestParam("sphone") String  sphone,@RequestParam("stuid") String stuid){
        Map<String,Object> result=new HashMap<>();
        Integer i=userInfoService.updateStudentPhone(sphone,stuid);
        if(i==0){
            result.put("msg",0);
        }else{
            result.put("msg",1);
        }
        return result;


    }
    /**
     * 修改老师的性别
     * @Author lzx
     * @paramid和sex
     * @return 是否成功 成功为1 失败为0
     */
    @ApiOperation("修改老师的性别")
    @PostMapping("wx/updateTeacherSex")
    Map<String,Object> updateTeacherSex(@RequestParam("tid") String  tid,@RequestParam("sex") Integer sex){
        Map<String,Object> result=new HashMap<>();
        Integer i=userInfoService.updateTeacherSex(tid,sex);
        if(i==0){
            result.put("msg",0);
        }else{
            result.put("msg",1);
        }
        return result;


    }
    /**
     * 修改老师手机号
     * @Author lzx
     * @paramid和sex
     * @return 是否成功 成功为1 失败为0
     */
    @ApiOperation("修改老师手机号")
    @PostMapping("wx/updateTeacherPhone")
    Map<String,Object> updateTeacherPhone(@RequestParam("phone") String  phone,@RequestParam("tid") String tid){
        Map<String,Object> result=new HashMap<>();
        Integer i=userInfoService.updateTeacherPhone(phone,tid);
        if(i==0){
            result.put("msg",0);
        }else{
            result.put("msg",1);
        }
        return result;


    }


    /**
     * @Description 得到所有学年
     * @Return
     * @Author dm
     * @Date Created in 2023/7/27 21:28
     **/
    @GetMapping("wx/getAllSchoolYears")
    List<String> getAllSchoolYears() {
        return schoolYearMapper.querySchoolYears();
    }

    /**
     * @Description 查询x轴坐标数据
     * @Return
     * @Author dm
     * @Date Created in 2023/7/28 0:24
     **/
    @GetMapping("wx/getXaries")
    List<String> getSelectXries(String school_year, String week) {
        List<String> xries = schoolYearMapper.queryXries(school_year, week);
        for (int i = 0; i < xries.size(); i++) {
            xries.set(i, xries.get(i).substring(5));
        }
        return xries;
    }

    /**
     * @Description 得到具体的老师考勤信息
     * @Return
     * @Author dm
     * @Date Created in 2023/7/28 16:39
     **/
    @GetMapping("wx/queryDetailHis")
    Result queryHisDetail(AttendanceHistory attendanceHistory,
                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return attendanceRecordsService.queryHistory(attendanceHistory, pageNum, pageSize);
    }

    /**
     * @Description 查询某一天的考勤数据
     * @Return
     * @Author dm
     * @Date Created in 2023/7/28 16:49
     **/
    @GetMapping("wx/getHisData")
    Result getHisData(String clas, String time, String username) {
        Integer id = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username)).getId();
        List<String> sids = attendanceMapper.queryHisData(clas, time, id + "");
        List<AttendanceData> list = new ArrayList<>();
        sids.forEach(sid -> {
            List<AttendanceData> attendanceData = attendanceMapper.queryAttendanceDatas(sid);
            if (attendanceData.size() >= 1) {
                list.addAll(attendanceData);
            }
        });
        return Result.success(list);
    }



}
